from APP import db,create_app
from APP.models.alert_list import alert_list  # 修正导入路径
from datetime import datetime, timedelta
# 创建Flask应用实例
app = create_app()

# 在应用上下文中执行数据库操作
with app.app_context():
    time_threshold = datetime.now() - timedelta(minutes=2)
    print("11111111111111111111")
    recent_alerts = db.session.query(alert_list).filter(
                    alert_list.time >= time_threshold).all()
    # 转换为可读字典格式
    readable_alerts = [{
        'id': alert.id,
        '类型': alert.type_id,
        '状态': alert.status,
        '标题': alert.title,
        '内容': alert.content,
        '监测点': alert.field,
        '时间': alert.time.strftime("%Y/%m/%d %H:%M:%S")  # 格式化时间
    } for alert in recent_alerts]

    print("最近告警列表：", readable_alerts)

